(function (factory) {
  // Packaging/modules magic dance
  var L;
  if (typeof define === 'function' && define.amd) {
    // AMD
    define(['leaflet'], factory);
  } else if (typeof module !== 'undefined') {
    // Node/CommonJS
    L = require('leaflet');
    module.exports = factory(L);
  } else {
    // Browser globals
    if (typeof window.L === 'undefined') {
      throw new Error('Leaflet must be loaded first');
    }
    factory(window.L);
  }
}(function (L) {
  'use strict';

  L.Control.Drawwin = (function () {

    var Drawwin = L.Control.extend({
      options: {
        position: 'topright',
        _divedit:null,
        _divcont:null,
        _amarker:null,
        _apolyline:null,
        _acircle:null,
        _arect:null,
        _apolygon:null,
        _aenddraw:null,
        _acleardrawlayer:null,
        _aclose:null
      },
      //初始化参数
      initialize: function (options) {
        L.Util.extend(this.options, options);
      },
      onAdd: function (map) {
        this._map = map;
        //创建一个class为leaflet-control-clegend的div，作用整体容器，可以同 this._container.appendChild 向此容器中添加任何内容
        this._container = L.DomUtil.create('div', 'leaflet-control-drawwin');

        //创建自己的内容
        this._divedit=L.DomUtil.create('div', 'leaflet-draw-control-edit-div');
        this._divedit.style="width:29px;height:29px;border:2px solid #889ebe; background-color:#ffffff;text-align:center;vertical-align:middle;";
        this._divedivimg=L.DomUtil.create('img', 'leaflet-draw-control-edit-div-img',this._divedit);
        this._divedivimg.style="width:24px;height:24px;margin-top:3px;background-image:url('/static/js/draw-lib/images/map_control_draw.png')";
        L.DomEvent.addListener(this._divedit, 'click', this.ShowToolBar, this);

        this._divcont=L.DomUtil.create('div', 'leaflet-draw-control-cont-div');
        this._divcont.style="height:29px;border:2px solid #889ebe; background-color:#ffffff;text-align:center;vertical-align:middle;";
        L.DomUtil.addClass(this._divcont,"leaflet-draw-control-hide");
        this._amarker=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._amarker.href = '#';
        this._amarker.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._amarker.innerHTML="点";
        L.DomEvent.addListener(this._amarker, 'click', this.drawMarker, this);

        this._apolyline=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._apolyline.href = '#';
        this._apolyline.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._apolyline.innerHTML="线";
        L.DomEvent.addListener(this._apolyline, 'click', this.drawPolyline, this);

        this._acircle=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._acircle.href = '#';
        this._acircle.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._acircle.innerHTML="圆";
        L.DomEvent.addListener(this._acircle, 'click', this.drawCircle, this);

        this._arect=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._arect.href = '#';
        this._arect.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._arect.innerHTML="矩形";
        L.DomEvent.addListener(this._arect, 'click', this.drawRect, this);

        this._apolygon=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._apolygon.href = '#';
        this._apolygon.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._apolygon.innerHTML="多边形";
        L.DomEvent.addListener(this._apolygon, 'click', this.drawPolygon, this);

        this._aenddraw=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._aenddraw.href = '#';
        this._aenddraw.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._aenddraw.innerHTML="结束绘制";
        L.DomEvent.addListener(this._aenddraw, 'click', this.endDraw, this);

        this._acleardrawlayer=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._acleardrawlayer.href = '#';
        this._acleardrawlayer.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._acleardrawlayer.innerHTML="清除";
        L.DomEvent.addListener(this._acleardrawlayer, 'click', this.clearDarwLayer, this);

        this._aclose=L.DomUtil.create('a', 'leaflet-draw-control-a',this._divcont);
        this._aclose.href = '#';
        this._aclose.style= "line-height:29px;height:29px;width:20px;margin-right:5px;margin-left:5px;";
        this._aclose.innerHTML="返回";
        L.DomEvent.addListener(this._aclose, 'click', this.HideToolBar, this);

        L.DomUtil.addClass(this._aenddraw,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");

        this._container.appendChild(this._divedit);
        this._container.appendChild(this._divcont);

        //返回
        return this._container;
      },
      onRemove: function (map) {

      },
      SetADefaultColor:function () {
        L.DomUtil.removeClass(this._amarker,"leaflet-draw-control-a-select");
        L.DomUtil.removeClass(this._apolyline,"leaflet-draw-control-a-select");
        L.DomUtil.removeClass(this._acircle,"leaflet-draw-control-a-select");
        L.DomUtil.removeClass(this._arect,"leaflet-draw-control-a-select");
        L.DomUtil.removeClass(this._apolygon,"leaflet-draw-control-a-select");
      },
      ShowToolBar:function () {
        L.DomUtil.addClass(this._divedit,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._divcont,"leaflet-draw-control-hide");
      },
      HideToolBar:function () {
        L.DomUtil.addClass(this._divcont,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._divedit,"leaflet-draw-control-hide");
      },
      drawMarker:function () {
        setTimeout(() => { this._map.fire('onDrawWinClickMarker', {});}, 500);//激发事件，因为点击后会直接触发了地图的click事件，所以需要加延时
        L.DomUtil.removeClass(this._aenddraw,"leaflet-draw-control-hide");
        if (!L.DomUtil.hasClass(this._acleardrawlayer,"leaflet-draw-control-hide")){
          L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        }
        this.SetADefaultColor();
        if (!L.DomUtil.hasClass(this._amarker,"leaflet-draw-control-a-select")){
          L.DomUtil.addClass(this._amarker,"leaflet-draw-control-a-select");
        }
        L.DomUtil.addClass(this._apolyline,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._acircle,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._arect,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolygon,"leaflet-draw-control-hide");
      },
      drawPolyline:function () {
        setTimeout(() => { this._map.fire('onDrawWinClickPolyline', {});}, 500);
        L.DomUtil.removeClass(this._aenddraw,"leaflet-draw-control-hide");
        if (!L.DomUtil.hasClass(this._acleardrawlayer,"leaflet-draw-control-hide")){
          L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        }
        this.SetADefaultColor();
        if (!L.DomUtil.hasClass(this._apolyline,"leaflet-draw-control-a-select")){
          L.DomUtil.addClass(this._apolyline,"leaflet-draw-control-a-select");
        }
        L.DomUtil.addClass(this._amarker,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._acircle,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._arect,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolygon,"leaflet-draw-control-hide");
      },
      drawCircle:function () {
        setTimeout(() => { this._map.fire('onDrawWinClickCircle', {});}, 500);
        L.DomUtil.removeClass(this._aenddraw,"leaflet-draw-control-hide");
        if (!L.DomUtil.hasClass(this._acleardrawlayer,"leaflet-draw-control-hide")){
          L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        }
        this.SetADefaultColor();
        if (!L.DomUtil.hasClass(this._acircle,"leaflet-draw-control-a-select")){
          L.DomUtil.addClass(this._acircle,"leaflet-draw-control-a-select");
        }
        L.DomUtil.addClass(this._amarker,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolyline,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._arect,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolygon,"leaflet-draw-control-hide");
      },
      drawRect:function () {
        setTimeout(() => { this._map.fire('onDrawWinClickRect', {});}, 500);
        L.DomUtil.removeClass(this._aenddraw,"leaflet-draw-control-hide");
        if (!L.DomUtil.hasClass(this._acleardrawlayer,"leaflet-draw-control-hide")){
          L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        }
        this.SetADefaultColor();
        if (!L.DomUtil.hasClass(this._arect,"leaflet-draw-control-a-select")){
          L.DomUtil.addClass(this._arect,"leaflet-draw-control-a-select");
        }
        L.DomUtil.addClass(this._amarker,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolyline,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._acircle,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolygon,"leaflet-draw-control-hide");
      },
      drawPolygon:function () {
        setTimeout(() => { this._map.fire('onDrawWinClickPolygon', {});}, 500);
        L.DomUtil.removeClass(this._aenddraw,"leaflet-draw-control-hide");
        if (!L.DomUtil.hasClass(this._acleardrawlayer,"leaflet-draw-control-hide")){
          L.DomUtil.addClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        }
        this.SetADefaultColor();
        if (!L.DomUtil.hasClass(this._apolygon,"leaflet-draw-control-a-select")){
          L.DomUtil.addClass(this._apolygon,"leaflet-draw-control-a-select");
        }
        L.DomUtil.addClass(this._amarker,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._apolyline,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._acircle,"leaflet-draw-control-hide");
        L.DomUtil.addClass(this._arect,"leaflet-draw-control-hide");
      },
      endDraw:function () {
        this._map.fire('onDrawWinEndDraw', {});
        L.DomUtil.addClass(this._aenddraw,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._acleardrawlayer,"leaflet-draw-control-hide");
        this.SetADefaultColor();
        L.DomUtil.removeClass(this._amarker,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._apolyline,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._acircle,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._arect,"leaflet-draw-control-hide");
        L.DomUtil.removeClass(this._apolygon,"leaflet-draw-control-hide");
      },
      clearDarwLayer:function () {
        this._map.fire('onDrawWinClearDrawLayer', {});
      }
    });

    return Drawwin;
  })();

  L.control.drawwin = function (options) {
    return new L.Control.Drawwin(options);
  };
}));
